package com.stenio.fancat.service.user;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.user.UserIconImage;
import com.stenio.fancat.form.user.UserIconImageForm;
import com.stenio.fancat.mapper.user.UserIconImageMapper;
import com.stenio.fancat.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * 用户头像实现类
 */
@Service
public class UserIconImageService extends ServiceImpl<UserIconImageMapper, UserIconImage> {

    @Autowired
    private UserIconImageMapper userIconImageMapper;

    @Autowired
    private OssService ossService;

    @Value("${oss.piggybank.endpoint}")
    private String piggybankEndPoint;

    /**
     * 通过用户ID获取图片信息
     *
     * @param userId
     *            用户图片实体类
     *
     * @return UserBO对象
     */
    public UserIconImage queryUserImageByCondition(String userId) {
        if (null == userId) {
            throw new RuntimeException("参数不能为空");
        }
        // 通过用户ID查询
        UserIconImage entity = new UserIconImage();
        entity.setUserId(userId);
        userIconImageMapper.selectOne(entity);
        return userIconImageMapper.selectOne(entity);
    }


    /**
     * 通过用户id修改头像图片
     * @param file
     * @param userIconImageForm
     * @return
     */
    public UserIconImage modifyUserIconImageByCondition(MultipartFile file,UserIconImageForm userIconImageForm){
        if (null == userIconImageForm) {
            throw new RuntimeException("参数不能为空");
        }
        // 通过用户ID和图片url修改
        UserIconImage image = new UserIconImage();
        Date date = new Date();
        image.setUpdateTime(date);
        image.setUserId(userIconImageForm.getUserId());
        try {
            ossService.updateFile(image.getImage(),file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return userIconImageMapper.selectOne(image);
    }

    /**
     * 保存图片到OSS服务器--->数据库
     * @param file
     * @return
     */
    public UserIconImage insertUserIconImageByCondition(MultipartFile file,UserIconImageForm userIconImageForm){
        if (null == userIconImageForm) {
            throw new RuntimeException("参数不能为空");
        }

        UserIconImage image = new UserIconImage();
        Date date = new Date();
        image.setCreateTime(date);
        image.setUpdateTime(date);
        image.setUserId(userIconImageForm.getUserId());
        try {
            String fileName = userIconImageForm.getUserId();
            image.setImage(piggybankEndPoint+fileName);
            ossService.putObject(fileName,file.getBytes());
            userIconImageMapper.insert(image);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

}
